#
#    Copyright (C) 2016 Francisco Javier Parra <franparpe@openmailbox.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#

from PyQt5.QtWidgets import QFrame, QComboBox
from PyQt5.QtWidgets import QGroupBox
from PyQt5.QtWidgets import QGridLayout

from PyQt5 import QtCore

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s


class FollowerType(QFrame):
    biConstWidth = "cam-follower bilateral with constant width"

    @classmethod
    def bilateralConstWidth(cls):
        return cls.biConstWidth
    
    def __init__(self, parent):
        "Initializing"
        super(FollowerType, self).__init__(parent)

        # Aesthetics
        self.setFrameShape(self.StyledPanel)
        self.setFrameShadow(self.Raised)

        # Group Box
        self.groupBox = QGroupBox(self)
        self.groupBox.setTitle(_fromUtf8("Follower type and its motion"))

        # The layout
        self.mainGrid = QGridLayout(self)
        self.mainGrid.addWidget(self.groupBox, 0, 0, 1, 1)

        # The combo box
        self.followerTypeSelector = QComboBox(self.groupBox)
        self._followerTypeSelectorContents()

        # new grid
        self.Grid4typeSelector = QGridLayout(self.groupBox)
        # adding combobox
        self.Grid4typeSelector.addWidget(self.followerTypeSelector,
                                         0, 0, 1, 1)

    def _followerTypeSelectorContents(self):
        """Initialize the comboBox contents
        """
        self.followerTypeSelector.clear()
        flatTrans = _fromUtf8("Flat-faced translating follower")
        circTrans = _fromUtf8("Circular-faced translating follower")
        flatRot = _fromUtf8("Flat-faced rotating follower")
        circRot = _fromUtf8("Circular-faced rotating follower")

        self.followerTypeSelector.addItem(flatTrans)
        self.followerTypeSelector.addItem(circTrans)
        self.followerTypeSelector.addItem(flatRot)
        self.followerTypeSelector.addItem(circRot)

    def getFollowerType(self):
        return self.followerTypeSelector.currentText()
        
    def connect(self, function):
        """connect the action with the combobox
        """
        self.followerTypeSelector.currentIndexChanged.connect(function)

    def setContents(self, text):
        if text == self.bilateralConstWidth():
            self.followerTypeSelector.setCurrentIndex(0)
            self.followerTypeSelector.setEnabled(False)
        else:
            self.followerTypeSelector.setEnabled(True)
